﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_Common;
using V5_Model;
using V5_BLL;

namespace V5_EShop.Admin
{
    public partial class Admin_Config : BasicPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Action = Input.SetString(Request["Action"]).Trim().ToLower();
                Type = Input.SetString(Request["Type"]).Trim().ToLower();
                Bind_TabSelect(Type);
                switch (Action)
                {
                    //case "edit":
                    //    this.MultiViewList.ActiveViewIndex = 1;
                    //    break;
                    case "m1":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        Bind_ConfigEdit();
                        break;
                }
            }
        }

        private void Bind_ConfigEdit()
        {
            this.MultiViewList.ActiveViewIndex = 0;

            ModelConfig model = new BLLConfig().GetModel();
            if (model != null)
            {
                this.hfID.Value = model.ID.ToString();
                this.txtSiteTitle.Text = model.SiteTitle;
                this.txtSiteUrl.Text = model.SiteUrl;
                this.txtWebmasterName.Text = model.WebmasterName;
                this.txtWebmasterEmail.Text = model.WebmasterEmail;
                this.txtSiteCopyright.Text = model.SiteCopyright;
                this.txtSiteDescription.Text = model.SiteDescription;
                this.txtSiteKeywords.Text = model.SiteKeywords;
                this.rblSiteState.SelectedValue = model.SiteState.ToString();
                this.txtSiteCloseText.Text = model.SiteCloseText;
                this.ddlMailObject.SelectedValue = model.MailObject.ToString();
                this.txtMailServer.Text = model.MailServer;
                this.txtMailServerUserName.Text = model.MailServerUserName;
                this.txtMailServerPassWord.Text = model.MailServerPassWord;
                this.txtMailDomain.Text = model.MailDomain;
                //
                //this.ddlUrlRewrite.SelectedIndex = this.ddlUrlRewrite.Items.IndexOf(this.ddlUrlRewrite.Items.FindByText(model.UrlRewrite));
                //
                this.txtUploadType.Text = model.UploadType;
                this.txtUploadMwidth.Text = model.UploadMwidth;
                this.txtUploadSwidth.Text = model.UploadSwidth;
                //
                this.txtSiteLogo.Text = model.SiteLogo;
            }
        }
        private void Bind_TabSelect(string type)
        {
            switch (type)
            {
                case "ispassed":
                    TabSelect2 = "select";
                    break;
                case "nopassed":
                    TabSelect3 = "select";
                    break;
                case "isalert":
                    TabSelect4 = "select";
                    break;
                default:
                    TabSelect1 = "select";
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ModelConfig model = new BLLConfig().GetModel();
            int ID = Input.SetInt(this.hfID.Value);
            //基本配置
            string SiteTitle = this.txtSiteTitle.Text;
            string SiteUrl = this.txtSiteUrl.Text;
            string WebmasterName = this.txtWebmasterName.Text;
            string WebmasterEmail = this.txtWebmasterEmail.Text;
            string SiteCopyright = this.txtSiteCopyright.Text;
            string SiteDescription = this.txtSiteDescription.Text;
            string SiteKeywords = this.txtSiteKeywords.Text;
            int SiteState = int.Parse(this.rblSiteState.SelectedValue);
            string SiteCloseText = this.txtSiteCloseText.Text;
            int MailObject = int.Parse(this.ddlMailObject.SelectedValue);
            string MailServer = this.txtMailServer.Text;
            string MailServerUserName = this.txtMailServerUserName.Text;
            string MailServerPassWord = this.txtMailServerPassWord.Text;
            string MailDomain = this.txtMailDomain.Text;
            //
            string urlrewrite = "aspx";// this.ddlUrlRewrite.SelectedValue;
            string uploadtype = this.txtUploadType.Text;
            string uploadmwidth = this.txtUploadMwidth.Text;
            string uploadswidth = this.txtUploadSwidth.Text;
            //
            string SiteLogo = this.txtSiteLogo.Text;
            if (ID > 0)
            {
                model.ID = ID;
                model.SiteTitle = SiteTitle;
                model.SiteUrl = SiteUrl;
                model.WebmasterName = WebmasterName;
                model.WebmasterEmail = WebmasterEmail;
                model.SiteCopyright = SiteCopyright;
                model.SiteDescription = SiteDescription;
                model.SiteKeywords = SiteKeywords;
                model.SiteState = SiteState;
                model.SiteCloseText = SiteCloseText;
                model.MailObject = MailObject;
                model.MailServer = MailServer;
                model.MailServerUserName = MailServerUserName;
                model.MailServerPassWord = MailServerPassWord;
                model.MailDomain = MailDomain;
                //
                model.UrlRewrite = urlrewrite;
                model.UploadType = uploadtype;
                model.UploadMwidth = uploadmwidth;
                model.UploadSwidth = uploadswidth;
                //
                model.SiteLogo = SiteLogo;

                new BLLConfig().Update(model);
                Response.Redirect(HTTPS.ComeUrl);
            }
        }

        protected void MenuList_MenuItemClick(object sender, MenuEventArgs e)
        {
            this.MultiViewList.ActiveViewIndex = Input.SetInt(e.Item.Value);
        }
    }
}
